package com.admin.system.modules.mnt.rest;

import com.admin.common.utils.PageResult;
import com.admin.component.web.MultipartFileUtil;
import com.admin.logging.annotation.Log;
import com.admin.logging.enums.OperateTypeEnum;
import com.admin.system.modules.mnt.dto.AppQueryDTO;
import com.admin.system.modules.mnt.entity.App;
import com.admin.system.modules.mnt.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author sq-lan
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "运维：应用管理")
@RequestMapping("/api/app")
public class AppController {

    private final AppService appService;

    @ApiOperation(value = "查询")
    @GetMapping
    @PreAuthorize("@ss.check('app:list')")
    public ResponseEntity<PageResult<App>> paging(AppQueryDTO dto) {
        return new ResponseEntity<>(appService.paging(dto), HttpStatus.OK);
    }

    @Log(module = "应用管理", operateType = OperateTypeEnum.ADD)
    @ApiOperation(value = "新增")
    @PostMapping
    @PreAuthorize("@ss.check('app:add')")
    public ResponseEntity<Object> add(@Validated @RequestBody App resources) {
        appService.add(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log(module = "应用管理", operateType = OperateTypeEnum.MODIFY)
    @ApiOperation(value = "修改")
    @PutMapping
    @PreAuthorize("@ss.check('app:edit')")
    public ResponseEntity<Object> modify(@Validated @RequestBody App resources) {
        appService.modify(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log(module = "应用管理", operateType = OperateTypeEnum.REMOVE)
    @ApiOperation(value = "删除")
    @DeleteMapping
    @PreAuthorize("@ss.check('app:del')")
    public ResponseEntity<Object> remove(@RequestBody Set<Integer> ids) {
        appService.remove(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出")
    @GetMapping(value = "/download")
    @PreAuthorize("@ss.check('app:list')")
    public void export(HttpServletResponse response, AppQueryDTO dto) throws IOException {
        List<App> apps = appService.list(dto);
        List<Map<String, Object>> list = new ArrayList<>();
        for (App app : apps) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("应用名称", app.getName());
            map.put("端口", app.getPort());
            map.put("上传目录", app.getUploadPath());
            map.put("部署目录", app.getDeployPath());
            map.put("备份目录", app.getBackupPath());
            map.put("启动脚本", app.getStartScript());
            map.put("部署脚本", app.getDeployScript());
            map.put("创建日期", app.getCreateTime());
            list.add(map);
        }
        MultipartFileUtil.downloadExcel(list, response);
    }
}
